

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.controls {
  public partial class paging : System.Web.UI.UserControl {

    #region Member Variables

    private PagedDataSource _pagedDataSource;
    private int categoryId = 0;
    private string searchTerms = string.Empty;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        categoryId = Utility.GetIntParameter("cid");
        searchTerms = Utility.GetParameter("searchTerms");

        int startNumber = 0;
        int endNumber = 0;

        startNumber = ((PagedDataSource.CurrentPageIndex * PagedDataSource.PageSize) + 1);
        endNumber = ((PagedDataSource.CurrentPageIndex + 1) * PagedDataSource.PageSize);
        if (endNumber > PagedDataSource.DataSourceCount) {
          endNumber = PagedDataSource.DataSourceCount;
        }
        lblShowingTotals.Text = string.Format(LocalizationUtility.GetText("lblShowingTotals"), startNumber, endNumber, PagedDataSource.DataSourceCount);

        for (int i = 0; i < PagedDataSource.PageCount; i++) {
          if (PagedDataSource.CurrentPageIndex == i) {
            pageLinks.InnerHtml += (i + 1) + "&nbsp;&nbsp;";
          }
          else {
            if (string.IsNullOrEmpty(searchTerms)) {
              pageLinks.InnerHtml += string.Format("<a href=\"catalog.aspx?cid={0}&p={1}\" class=\"pageLink\">{2}</a>&nbsp;&nbsp;", categoryId, i, (i + 1));
            }
            else {
              pageLinks.InnerHtml += string.Format("<a href=\"search.aspx?searchTerms={0}&p={1}\" class=\"pageLink\">{2}</a>&nbsp;&nbsp;", searchTerms, i, (i + 1));
            }
          }
        }
        hlPrevious.Visible = !PagedDataSource.IsFirstPage;
        if (hlPrevious.Visible) {
          if (string.IsNullOrEmpty(searchTerms)) {
            hlPrevious.NavigateUrl = string.Format("~/catalog.aspx?cid={0}&p={1}", categoryId, (PagedDataSource.CurrentPageIndex - 1));
          }
          else {
            hlPrevious.NavigateUrl = string.Format("~/search.aspx?searchTerms={0}&p={1}", searchTerms, (PagedDataSource.CurrentPageIndex - 1));
          }
        }
        hlNext.Visible = !PagedDataSource.IsLastPage;
        if (hlNext.Visible) {
          if (string.IsNullOrEmpty(searchTerms)) {
            hlNext.NavigateUrl = string.Format("~/catalog.aspx?cid={0}&p={1}", categoryId, (PagedDataSource.CurrentPageIndex + 1));
          }
          else {
            hlNext.NavigateUrl = string.Format("~/search.aspx?searchTerms={0}&p={1}", searchTerms, (PagedDataSource.CurrentPageIndex + 1));
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(paging).Name + ".Page_Load", ex);
        throw;
      }
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the paged data source.
    /// </summary>
    /// <value>The paged data source.</value>
    public PagedDataSource PagedDataSource {
      get {
        return _pagedDataSource;
      }
      set {
        _pagedDataSource = value;
      }
    }

    #endregion

  }
}